﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminBackgroundController : Controller
    {
        //
        // GET: /AdminCP/AdminBackground/

        #region "Edit"
        public ActionResult Edit()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                Background model = new Background();

                EntityBackground background = db.EntityBackgrounds.FirstOrDefault();
                model.ID = background.ID;
                model.Image = background.Image;
                model.Show = background.Show;

                return View(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Background model, HttpPostedFileBase Image)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityBackground background = db.EntityBackgrounds.Where(p => p.ID == model.ID).FirstOrDefault();
                if (background != null)
                {
                    background.Show = model.Show;

                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (background.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/cssFrontEnd/images/"), model.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/cssFrontEnd/images/"), encodestring);
                        Image.SaveAs(path);

                        background.Image = encodestring;
                    }

                    db.SubmitChanges();
                    TempData["EditSuccessfully"] = "You have successfully changed";
                    return RedirectToAction("Edit", "AdminBackground");
                }
                TempData["EditSuccessfully"] = "You have unsuccessfully changed";
                return RedirectToAction("Edit", "AdminBackground");
            }
        }

        #endregion //edit

    }
}
